/**  
 * All rights Reserved, Designed By www.beonelot.com
 * @Package: com.bccah.modlib.dbdo.controller
 * @Filename: AcsControlVisitorMethodController.java
 * @Description: TODO(AcsControlVisitorMethodController)
 * @author: 金鑫智慧
 * @date: 2020-04-08
 * @version: V1.0
 * @Copyright: 2018 www.beonelot.com Inc. All rights reserved.
 * 注意：本内容仅限于重庆金鑫智慧科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
package com.jinxin.platform.cdacsrecord.core.controller;

import com.github.pagehelper.PageInfo;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.AcsControlVisitorMethodDO;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.CommonCodeEntity;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.ResultEntity;
import com.jinxin.platform.cdacsrecord.core.pojo.vo.AcsControlVisitorMethodVO;
import com.jinxin.platform.cdacsrecord.core.service.AcsControlVisitorMethodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO(AcsControlVisitorMethodController)
 * @Classn: AcsControlVisitorMethodController
 * @author SY
 * @date 2020-04-08
 */
@RestController
@RequestMapping("/basicVisitorMethod")
@Slf4j
public class AcsControlVisitorMethodController {

    @Autowired
    private AcsControlVisitorMethodService visitorMethodService;

    /**
     * 获取访问方式信息的分页表
     * @param typeName
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/typeTable")
    public Map getTypeTable(String typeName, int page, int limit  ){
        Map<String,Object> resultMap = new HashMap<>();
        PageInfo<AcsControlVisitorMethodDO> pageInfo = visitorMethodService.selParamPage(page,limit,typeName );
        resultMap.put("code","0");
        resultMap.put("msg","");
        resultMap.put("count",pageInfo.getTotal());
        resultMap.put("data",pageInfo.getList());
        return resultMap;
    }

    /**
     * 添加访问方式信息
     * @param infoVO
     * @return
     */
    @PostMapping("/addType")
    public ResultEntity addTypeInfo(@Valid AcsControlVisitorMethodVO infoVO)throws Exception{
        // save
        AcsControlVisitorMethodDO methodDO = new AcsControlVisitorMethodDO();
        methodDO.setVisitorMethodKey(infoVO.getVisitorMethodKey() );
        List<AcsControlVisitorMethodDO> visitorMethodDO = visitorMethodService.selDOByInfo(methodDO);
        if(visitorMethodDO != null &&visitorMethodDO.size()>0 ){
            return new ResultEntity(CommonCodeEntity.ALREADY_EXIST,"KEY值已存在","");
        }
        AcsControlVisitorMethodDO infoDO = new AcsControlVisitorMethodDO();
        BeanUtils.copyProperties(infoVO,infoDO );
        infoDO.setCreateTime(LocalDateTime.now());
        visitorMethodService.saveDO(infoDO);
        log.info(infoDO.toString());
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc","");
    }

    /**
     * 删除访问方式信息
     * @param bid
     * @return
     */
    @PostMapping("/delType")
    public ResultEntity delType(String bid  ){
        visitorMethodService.delDO(bid);
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc","" );
    }

    /**
     * 更新方式信息
     * @param infoVO
     * @return
     */
    @PostMapping("/upType")
    public ResultEntity upType(@Valid AcsControlVisitorMethodVO infoVO){
        if(StringUtils.isEmpty(infoVO.getVisitorMethodId() )){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"visitorMethodId为空","" );
        }
        AcsControlVisitorMethodDO infoDO = new AcsControlVisitorMethodDO();
        BeanUtils.copyProperties(infoVO,infoDO );
        log.info(infoDO.toString());
        visitorMethodService.updateDO(infoDO);
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc","" );
    }

    /**
     * 获取访问方式记录
     * @return
     */
    @PostMapping("/selTypeList")
    public ResultEntity selTypeList(String key ){
        AcsControlVisitorMethodDO methodDO = new AcsControlVisitorMethodDO();
        methodDO.setVisitorMethodKey(key);
        List<AcsControlVisitorMethodDO> infoDOList = visitorMethodService.selDOByInfo(methodDO);
        return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc",infoDOList);
    }

}
